package com.jvm.outofmemoryerror;

import com.sun.xml.internal.ws.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;

/**
 * @author: csh
 * @Date: 2021/4/13 18:24
 * @Description: 测试方法区溢出
 * 1.6
 * -XX:permSize=10m -XX:MaxPermSize=10m
 *
 * 1.8及以上
 * -XX:MetaspaceSize=10m -XX:MaxMetaspaceSize=10m
 */
public class OOMTest extends ClassLoader {


    public static void main(String[] args) {
        int j =0;
        try {
            OOMTest test = new OOMTest();
            for(int i =0;i<10000;i++){
                //创建classWriter对象，用于生成类的二进制字节码
                ClassWriter classWriter = new ClassWriter(0);
                //类名
                classWriter.visit(Opcodes.V1_8,Opcodes.ACC_PUBLIC,"Class"+i,null,"java/lang/Object",null);
                //返回byte[]
                byte[] code = classWriter.toByteArray();
                //类的加载
                test.defineClass("Class"+i,code,0,code.length);
                j++;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println(j);
        }
    }
}
